﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Drawing.Printing;

namespace XBoshiGFS.forms
{
    public partial class PageSetUp : Form
    {
        public PageSetUp()
        {
            InitializeComponent();

            String strfcp = (String)Registry.GetValue("HKEY_CURRENT_USER\\SOFTWARE\\Xboshi\\XBoshiGFS", "firstCorrectPages", "1");
            String strscp = (String)Registry.GetValue("HKEY_CURRENT_USER\\SOFTWARE\\Xboshi\\XBoshiGFS", "secondCorrectPages", "10000");
            String strrpp = (String)Registry.GetValue("HKEY_CURRENT_USER\\SOFTWARE\\Xboshi\\XBoshiGFS", "recordPerPages", "1");
            String strprint = (String)Registry.GetValue("HKEY_CURRENT_USER\\SOFTWARE\\Xboshi\\XBoshiGFS", "billPrinter", "");



            bill1Height.Text =(String)Registry.GetValue("HKEY_CURRENT_USER\\SOFTWARE\\Xboshi\\XBoshiGFS", "bill1Height","0");
            bill1Width.Text=(String)Registry.GetValue("HKEY_CURRENT_USER\\SOFTWARE\\Xboshi\\XBoshiGFS", "bill1Width", "0");
            bill2Height.Text=(String)Registry.GetValue("HKEY_CURRENT_USER\\SOFTWARE\\Xboshi\\XBoshiGFS", "bill2Height","0");
            bill2Width.Text=(String)Registry.GetValue("HKEY_CURRENT_USER\\SOFTWARE\\Xboshi\\XBoshiGFS", "bill2Width","0");

            tbMarginLeft.Text = (String)Registry.GetValue("HKEY_CURRENT_USER\\SOFTWARE\\Xboshi\\XBoshiGFS", "bill1MarginLeft", "0");
            tbMarginTop.Text = (String)Registry.GetValue("HKEY_CURRENT_USER\\SOFTWARE\\Xboshi\\XBoshiGFS", "bill1MarginTop", "0");

            tbMarginRight.Text = (String)Registry.GetValue("HKEY_CURRENT_USER\\SOFTWARE\\Xboshi\\XBoshiGFS", "bill1MarginRight", "0");
            tbMarginBottom.Text = (String)Registry.GetValue("HKEY_CURRENT_USER\\SOFTWARE\\Xboshi\\XBoshiGFS", "bill1MarginBottm", "0");
            String strPrintType = (String)Registry.GetValue("HKEY_CURRENT_USER\\SOFTWARE\\Xboshi\\XBoshiGFS", "printType", "1");


            tbFirst.Text = strfcp;
            tbSecond.Text = strscp;
            tbRowPP.Text = strrpp;

            //进行循环检查纸张设置
            System.Drawing.Printing.PrintDocument doc = new System.Drawing.Printing.PrintDocument();
            foreach (System.Drawing.Printing.PaperSize paper in doc.PrinterSettings.PaperSizes)
            {

                if (paper.PaperName.ToUpper().Equals("BillReport".ToUpper()))
                {
                    billReport.ForeColor = Color.Black;  
                }
                if (paper.PaperName.ToUpper().Equals("BillReport1".ToUpper()))
                {
                    billReport1.ForeColor = Color.Black;
                }
            }

            if (String.IsNullOrWhiteSpace(strprint)){
                //设为默认打印机
                strprint=doc.PrinterSettings.PrinterName ;  
            
            }

            foreach (String strPrinter in PrinterSettings.InstalledPrinters )
            //在列表框中列出所有的打印机, 
            {
                this.cmbPrinter.Items.Add(strPrinter);
            }

            this.cmbPrinter.SelectedItem = strprint;
            this.cmbPrintType.SelectedIndex=int.Parse(strPrintType)-1;  


        }

        private void PageSetUp_Load(object sender, EventArgs e)
        {

        }

        private void btnSave_Click(object sender, EventArgs e)
        {

                Registry.SetValue("HKEY_CURRENT_USER\\SOFTWARE\\Xboshi\\XBoshiGFS", "billPrinter", cmbPrinter.SelectedItem.ToString());
                Registry.SetValue("HKEY_CURRENT_USER\\SOFTWARE\\Xboshi\\XBoshiGFS", "firstCorrectPages", tbFirst.Text);
                Registry.SetValue("HKEY_CURRENT_USER\\SOFTWARE\\Xboshi\\XBoshiGFS", "secondCorrectPages", tbSecond.Text);
                Registry.SetValue("HKEY_CURRENT_USER\\SOFTWARE\\Xboshi\\XBoshiGFS", "recordPerPages", tbRowPP.Text );

                Registry.SetValue("HKEY_CURRENT_USER\\SOFTWARE\\Xboshi\\XBoshiGFS", "bill1Height", bill1Height.Text  );
                Registry.SetValue("HKEY_CURRENT_USER\\SOFTWARE\\Xboshi\\XBoshiGFS", "bill1Width",  bill1Width.Text);
                Registry.SetValue("HKEY_CURRENT_USER\\SOFTWARE\\Xboshi\\XBoshiGFS", "bill2Height", bill2Height.Text);
                Registry.SetValue("HKEY_CURRENT_USER\\SOFTWARE\\Xboshi\\XBoshiGFS", "bill2Width", bill2Width.Text);
                Registry.SetValue("HKEY_CURRENT_USER\\SOFTWARE\\Xboshi\\XBoshiGFS", "printType", cmbPrintType.SelectedItem.ToString().Substring(0,2).Trim () );

                Registry.SetValue("HKEY_CURRENT_USER\\SOFTWARE\\Xboshi\\XBoshiGFS", "bill1MarginLeft", tbMarginLeft.Text);
                Registry.SetValue("HKEY_CURRENT_USER\\SOFTWARE\\Xboshi\\XBoshiGFS", "bill1MarginTop", tbMarginTop.Text);
                Registry.SetValue("HKEY_CURRENT_USER\\SOFTWARE\\Xboshi\\XBoshiGFS", "bill1MarginRight", tbMarginRight.Text);
                Registry.SetValue("HKEY_CURRENT_USER\\SOFTWARE\\Xboshi\\XBoshiGFS", "bill1MarginBottm", tbMarginBottom.Text);
            
            
            this.Close();
        }

    }
}